﻿using Microsoft.EntityFrameworkCore;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Model
{
    public class SqlSystem : Isystem
    {
        private readonly AppDbContext appDbContext;
        public SqlSystem(AppDbContext appDb)
        {
            this.appDbContext = appDb;
        }
        //人员管理删除
        public int Delete(int id)
        {
            Personnel a = appDbContext.Personnel.Find(id);
            a.State_ID = 2;
            appDbContext.Personnel.Update(a);
            return appDbContext.SaveChanges();
        }

        //部门下拉框数据
        public IEnumerable<Department> Departments()
        {
            return appDbContext.Department;
        }

        //人员显示
        public  IEnumerable<Personnel> personnels()
        {
            var list = appDbContext.Personnel.Include(a => a.Position.Department).Include(b => b.State).Include(c => c.Position);
            return list;
        }
        public IEnumerable<Personnel> personnel()
        {
            var list = appDbContext.Personnel.Include(a=>a.Role).Where(x=>x.State_ID == 1);
            return list;
        }
        //职位下拉框数据
        public IEnumerable<Position> positions()
        {
            return appDbContext.Position;
        }
        //状态下拉框数据
        public IEnumerable<Tstate> SelStates()
        {
            return appDbContext.Tstate;
        }
        //人员添加
        public int Add(Personnel p)
        {
           Personnel pe = new Personnel{
               staff_NAME=p.staff_NAME,
               staff_CODE= p.staff_CODE,
               staff_alias = p.staff_alias,
               username = p.username,
               password = MD5Helper.MD5Encrypt32(p.password),
               email = p.email,
               home_tel = p.home_tel,
               Address = p.Address,
               State_ID = 1,
               certificates = p.certificates,
               sex = p.sex,
               Entry = p.Entry,
               Job_ID = p.Job_ID,
               Role_ID = p.Role_ID,
               remarks = p.remarks
           };
            appDbContext.Personnel.Add(pe);
            return appDbContext.SaveChanges();
        }
        //角色管理
        public IEnumerable<Role> Roles()
        {
                IEnumerable<Role> list = appDbContext.Role;
                return list;      
        }
        //角色管理添加
        public int AddRole(Role r)
        {
            appDbContext.Role.Add(r);
            return appDbContext.SaveChanges();
        }
        //角色管理删除
        public int DelRoles(int id)
        {
            Role r = appDbContext.Role.Where(a => a.Role_ID == id).First();
            appDbContext.Role.Remove(r);
            return appDbContext.SaveChanges();
        }
        //角色修改
        public int UpdateRole(Role r)
        {
            Role ro = appDbContext.Role.Find(r.Role_ID);
            ro.Role_Name = r.Role_Name;
            ro.remarks = r.remarks;
            appDbContext.Role.Update(ro);
            return appDbContext.SaveChanges();
        }
        //职位管理
        public IEnumerable<Position> SelPositions()
        {
               return appDbContext.Position.Include(a => a.Department);
        }
        //职位管理添加
        public int AddPosition(Position p)
        {
            appDbContext.Position.Add(p);
            return appDbContext.SaveChanges();
        }
        //职位管理删除
        public int DelPosition(int id)
        {
            Position po= appDbContext.Position.Find(id);
            po.state_ID = 2;
            appDbContext.Position.Update(po);
            return appDbContext.SaveChanges();
        }


        //部门管理
        public IEnumerable<Department> SelDepartments()
        {
                IEnumerable<Department> list = appDbContext.Department;
                return list;
        }
        //部门删除
        public int DelDepartments(int id)
        {
            Department list = appDbContext.Department.Where(a => a.Depar_ID == id).First();
            appDbContext.Department.Remove(list);
            return appDbContext.SaveChanges();
        }
        //部门管理添加
        public int AddDepartments(Department d)
        {
            appDbContext.Department.Add(d);
            return appDbContext.SaveChanges();
        }

        public virtual IEnumerable<Personnel> Login(string user,string pwd)
        {
            return   appDbContext.Personnel.Where(x=>x.username== user && x.password== MD5Helper.MD5Encrypt32(pwd)&& x.State_ID==1).Include(a=>a.Role);
        }

        public virtual IEnumerable<Rights> Right(int id)
        {
         
            return appDbContext.Rights.Where(x => x.Role_ID == id);
        }

        public IEnumerable<Role> Roless()
        {
            return appDbContext.Role;
        }

        public int upersonnel(int pid, int id)
        {
            Personnel p = appDbContext.Personnel.Find(pid);
            p.Role_ID = id;
            appDbContext.Personnel.Update(p);
            return appDbContext.SaveChanges();
        }

        public int Updatep(Personnel p)
        {
            Personnel pl= appDbContext.Personnel.Find(p.staff_ID);
            pl.staff_NAME = p.staff_NAME;
            pl.staff_CODE =p.staff_CODE;
            pl.email =p.email;
            pl.home_tel = p.home_tel;
            pl.Address =p.Address;
            pl.certificates =p.certificates;
            pl.sex =p.sex;
            pl.Entry =p.Entry;
            appDbContext.Personnel.Update(pl);
            return appDbContext.SaveChanges();
        }

        //职位修改
        public int Update(Position p)
        {
            Position po = appDbContext.Position.Find(p.Job_ID);
            po.Job_Name = p.Job_Name;
            po.Depar_ID = p.Depar_ID;
            po.state_ID = p.state_ID;
            po.email = p.email;
            po.remarks = p.remarks;
            appDbContext.Position.Update(po);
            return appDbContext.SaveChanges();
        }
        //部门修改
        public int UpdateDepartment(Department d)
        {
            Department de = appDbContext.Department.Find(d.Depar_ID);
            de.Depar_NAME = d.Depar_NAME;
            de.Depar_tel = d.Depar_tel;
            de.reamkers = d.reamkers;
            appDbContext.Department.Update(de);
            return appDbContext.SaveChanges();
        }
    }
}
